<template>
    <div class="loginDiv">
        <div class="formDiv" @keyup.enter="login('loginForm')">
            <div class="imgDiv">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="25px" class="loginForm">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username" autocomplete="off"
                        placeholder="请填写用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-lock" type="password" v-model="loginForm.password" autocomplete="off"
                        placeholder="请填写密码"></el-input>
                </el-form-item>
                <el-form-item style="text-align: center;">
                    <el-button type="primary" @click="login('loginForm')">提交</el-button>
                    <el-button @click="resetForm('loginForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            loginForm: {
                username: '',
                password: '',
            },
            rules: {
                username: [
                    { required: true, message: '请填写用户名', trigger: 'blur' },
                    { max: 5, min: 3, message: '3-5个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请填写密码', trigger: 'blur' },
                    { max: 12, min: 6, message: '6-12个字符', trigger: 'blur' }
                ],
            }
        };
    },
    methods: {
        login(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    const { data: { data, meta } } = await this.$http.post('login', this.loginForm);
                    if (meta.status !== 200) return this.$message.error('登录失败');
                    sessionStorage.setItem('token', data.token);
                    sessionStorage.setItem('username', data.username);
                    sessionStorage.setItem('rid', data.rid);
                    this.$message.success('登录成功');
                    this.$router.replace('/main');
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {
        if (sessionStorage.getItem('username')) {
            this.loginForm.username = sessionStorage.getItem('username');
        }
    }
}

</script>
<style scoped lang="less">
.loginDiv {
    width: 100%;
    height: 100%;
    background-color: skyblue;
}

.formDiv {
    width: 30%;
    height: 300px;
    border: 1px solid black;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 10px 25px 0 0;
    box-sizing: border-box;
    border-radius: 8px;
    background-color: #ffff;
    padding-top: 100px;

    .imgDiv {
        width: 100px;
        height: 100px;
        position: absolute;
        top: 0;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: white;
        border-radius: 50%;
        border: 1px solid gray;
        padding: 8px;
        box-shadow: 0 0 15px gray;

        img {
            width: 100%;
            height: 100%;
        }
    }
}
</style>